package logics 
{
	//code 3- 6
	import constants.Events;
	import constants.Structures;
	import constants.utils;
	import flash.events.Event;
	import flash.geom.Point;
	import points.CollidePoint;
	import points.ConstructPoint;
	import structures.Structure;
	/**
	 * ...
	 * @author parasitic
	*/
	
	/* Consider in the future creating a nested movieclip to hold units
	 * 
	 * 
	 * */
	 
	 public class GameField extends Logic 
	{
		private var fieldmaps:Array; //a*
		private var storedPoints:Array;
		private var tempPoints:Array;
		private var camera:Camera;
		
		//Future Changes
		//change get field algorithm 
		//use region-based collision testing
		
		
		private function enterframe_handler(e:Event):void
		{
			for (var i:int = 0; i < tempPoints.length; i++)
			{
				//traces false when collides
				var flag:Boolean = storedPoints.every((tempPoints[i] as CollidePoint).testArray, null);
			}	
		}
		
		private function placebuilding_handler(e:CustomEvent):void
		{
			try
			{
				var struct:Structure = tempPoints[0] as Structure;
				if (struct.isValid())
				{
					//clean up
					
					//placeOnMap(struct); //Field Map - deprecated
					dispatchEvent( new CustomEvent(Events.FINISH_BUILD, struct));
					delListener(Event.ENTER_FRAME, enterframe_handler, false);
					delListener(Events.PLACE_BUILDING, placebuilding_handler);
					storedPoints.push( utils.arrayPopItem(tempPoints, struct));		
				}
			}
			catch (err:Error)
			{
				trace("placebuilding_handler: " + err);
			}
			
		}
		
		private function nowbuilding_handler(e:CustomEvent):void
		{
			try
			{
				if (e.args instanceof Structure)
				{
					tempPoints.push(e.args);	
					camera.addChild(e.args);
					newListener(Event.ENTER_FRAME, enterframe_handler, false);
					newListener(Events.PLACE_BUILDING, placebuilding_handler);
				}
			}
			catch (err:Error)
			{
				trace("nowbuilding_handler: " + err);
			}
		}
		
		private function moveCamera(dir:uint):void
		{
			switch(dir)
			{
				case 0:
					camera.moveLeft();
					break;
				
				case 1:
					camera.moveUp();
					break;
					
				case 2:
					camera.moveRight();
					break;
					
				case 3:
					camera.moveDown();
					break;
			}
		}
		
		private function cameramoving_handler(e:CustomEvent):void
		{
			var dir:uint = e.args;
			
			if (dir > utils.None && dir < 4)
			{
				moveCamera(dir);
			}
		}
		
		//Field Map - deprecated
		/*
		function placeOnMap(c:CollidePoint):void
		{
			var map:FieldMap;
			for (var index:uint = 0; index < fieldmaps.length; index++)
			{
				map = fieldmaps[0];
				trace(map.isMember(c));
			}
		}
		*/
		//Field Map - deprecated
		/*
		private function makeFieldMaps():void
		{
			fieldmaps = new Array();
			for (var i:uint = 0; i < 4; i++)
			{
				for (var j:uint = 0; j < 4; j++)
				{
					var offx:Number = -600 + (i * 400);
					var offy:Number = -600 + (j * 400);
					//trace(offx);
					fieldmaps.push( new FieldMap(offx, offy));
				}
			}
		}
		*/
		
		override protected function init(e:Event):void
		{
			super.init(e);
			
			//Field Map - deprecated
			//makeFieldMaps();
			
			storedPoints = new Array();
			tempPoints = new Array();
			camera = new Camera();
			camera.outline();
			addChild(camera);
			
			newListener(Events.NOW_BUILDING, nowbuilding_handler);
			newListener(Events.CAMERA_MOVING, cameramoving_handler);
		}
		
		public function GameField(s:StateMachine ) 
		{
			super(s);
		}
	}
}